package com.example.diagnodroid.mouvement;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import com.example.diagnodroid.R;

public class BounceView extends View {

	protected Drawable ball;
	protected Point ballPos;

	float mPitch;
	float mHeading;

	public BounceView(Context context, AttributeSet attrs) {

		super(context, attrs);

		setBackgroundResource(R.drawable.soccer_field);
		ballPos = new Point(0, 0);
		ball = getResources().getDrawable(R.drawable.soccerball);

	}

	@Override
	protected void onDraw(Canvas canvas) {
		synchronized (this) {

			ballPos.x -= mPitch*2;

			if (ballPos.x >= (getWidth() - ball.getBounds().width())) {

				ballPos.x = getWidth() - ball.getBounds().width();

			} else if (ballPos.x <= 0) {

				ballPos.x = 0;
			}

			ballPos.y += mHeading*2;

			if (ballPos.y >= (getHeight() - ball.getBounds().height())) {

				ballPos.y = getHeight() - ball.getBounds().height();

			} else if (ballPos.y <= 0) {

				ballPos.y = 0;
			}

			ball.setBounds(ballPos.x, ballPos.y,

			ballPos.x + 70, this.ballPos.y + 70);

			ball.draw(canvas);
		}

	}
}
